<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//列表页控制器
class ListController extends Controller{
	//列表页
	public function index(){

		//获得顶级分类,导航条
    		$cateModel =  new \Common\Model\Cate;
    		$data = $cateModel->where("pid=0")->get();
		View::with('topCate',$data);


		//标签*********
		//处理标签名称
		$tid = Q('get.tid',0,'intval');
		if($tid){
			$tagModel = new \Common\Model\Tag;
			$name = $tagModel->where("tid={$tid}")->pluck('tname');
			//统计文章总数
			$arcTagModel = new \Common\Model\ArcTag;
			$total = $arcTagModel->join('article','aid','=','article_aid')->where("is_recycle=0 and tag_tid=$tid")->count();
			//分页-分页类使用
			//分页使用方法：page在ListArcModel里面加条件  -> limit(Page::limit())  2处即可！
			$page = Page::row(5)->make($total);
			View::with('page',$page);
			//显示所有文章
			$ListArcModel = new \Common\Model\Arc;
			//数据库 SELECT * FROM article JOIN category ON category_cid=cid JOIN article_tag ON article=aid;
			$ListArcData=$ListArcModel->join('category','category_cid','=','cid')->join('article_tag','aid','=','article_aid')->where("is_recycle=0 and tag_tid={$tid}")->limit(Page::limit())->orderBy('sendtime','DESC')->get();
			
       		//实例中间表模型
			foreach ($ListArcData as $k =>$v){
				$data = $arcTagModel->join('tag','tag_tid','=','tid')->where("article_aid={$v['aid']}")->field('tid,tname')->get();
				$ListArcData[$k]['tag'] = $data;
				
			}
		}
		//分类***********
		$cid = Q('get.cid',0,'intval');
		//处理分类名称
		if($cid){
			$cateModel = new \Common\Model\Cate;
			$name = $cateModel->where("cid={$cid}")->pluck('cname');
			//统计该分类
			$cids = $cateModel->getSon($cateModel->get(),$cid);
			$cids[]=$cid;
			$arcModel=new \Common\Model\Arc;
			$total = $arcModel->where("category_cid in(". implode(',',$cids).")")->count();
			//分页-分页类使用
			$page = Page::row(5)->make($total);
			View::with('page',$page);
			//根据cid筛选出文章
			$ListArcData=$arcModel->join('category','category_cid','=','cid')->where("is_recycle=0 and category_cid={$cid}")->limit(Page::limit())->orderBy('sendtime','DESC')->get();
			$arcTagModel = new \Common\Model\ArcTag;
       		//实例中间表模型
			foreach ($ListArcData as $k =>$v){
				$data = $arcTagModel->join('tag','tag_tid','=','tid')->where("article_aid={$v['aid']}")->field('tid,tname')->get();
				$ListArcData[$k]['tag'] = $data;
			}
		}
		View::with('ListArcData',$ListArcData);



		//列表页right标签云
		$TagModel = new \Common\Model\Tag;
		$rightTag = $TagModel->get();
		View::with('rightTag',$rightTag);


		//列表页尾部-友情链接
		$LinkModel = new \Common\Model\Link;
		$FootLink = $LinkModel->get();
		View::with('FootLink',$FootLink);


		//列表页最新3篇文章
		$arcModel = new \Common\Model\Arc;
		$arcData = $arcModel->join('category','category_cid','=','cid')->orderBy('sendtime','DESC')->limit(5)->get();
		View::with('arcData',$arcData);



		
		//列表页right分类列表***********
		$cateModel =  new \Common\Model\Cate;
    	$Catedata = $cateModel->get();
    		//实例化文章模型
    	$ArcModel = new \Common\model\Arc;
    	foreach ($Catedata as $k => $v) {
    		$Catedata[$k]['num'] = $ArcModel->where("category_cid={$v['cid']}")->count();
    	}
    	// p($Catedata);
    	View::with('Catedata',$Catedata);




    	
		


		//名称
		View::with('name',$name);
		//共有多少篇文章
		View::with('total',$total);
		//right分类列表






		
		View::make();
	}
}







